#include "types.h"
#include "x86.h"
//#include "defs.h"//added by us


//void* malloc(uint);//added by us
//void free(void*);//added by us  



void*
memset(void *dst, int c, uint n)
{
  stosb(dst, c, n);
  return dst;
}

int
memcmp(const void *v1, const void *v2, uint n)
{
  const uchar *s1, *s2;
  
  s1 = v1;
  s2 = v2;
  while(n-- > 0){
    if(*s1 != *s2)
      return *s1 - *s2;
    s1++, s2++;
  }

  return 0;
}

void*
memmove(void *dst, const void *src, uint n)
{
  const char *s;
  char *d;

  s = src;
  d = dst;
  if(s < d && s + n > d){
    s += n;
    d += n;
    while(n-- > 0)
      *--d = *--s;
  } else
    while(n-- > 0)
      *d++ = *s++;

  return dst;
}

// memcpy exists to placate GCC.  Use memmove.
void*
memcpy(void *dst, const void *src, uint n)
{
  return memmove(dst, src, n);
}

int
strncmp(const char *p, const char *q, uint n)
{
  while(n > 0 && *p && *p == *q)
    n--, p++, q++;
  if(n == 0)
    return 0;
  return (uchar)*p - (uchar)*q;
}

char*
strncpy(char *s, const char *t, int n)
{
  char *os;
  
  os = s;
  while(n-- > 0 && (*s++ = *t++) != 0)
    ;
  while(n-- > 0)
    *s++ = 0;
  return os;
}

// Like strncpy but guaranteed to NUL-terminate.
char*
safestrcpy(char *s, const char *t, int n)
{
  char *os;
  
  os = s;
  if(n <= 0)
    return os;
  while(--n > 0 && (*s++ = *t++) != 0)
    ;
  *s = 0;
  return os;
}

int
strlen(const char *s)
{
  int n;

  for(n = 0; s[n]; n++)
    ;
  return n;
}
//our code
char *append(char *str1, char *str2){
  int strLen1 = strlen(str1);
  int strLen2 = strlen(str2);
  char *buildedString = (char*)malloc(strLen1 + strLen2 + 1);
  char c;
  while((c = *str1) != '\0'){
    *buildedString = c;
    str1++;
    buildedString++;
  }
  
  while((c = *str2) != '\0'){
    *buildedString = c;
    str2++;
    buildedString++;
  }
  *buildedString = '\0';
  return buildedString - (strLen1 + strLen2);
}
/*
char *appendChar(char *str1, char c){
  char * cString = (char *) malloc(2);
  cString[0] = c;
  cString[1] = '\0';
  char *buildedString;
  buildedString = append(str1,cString);
  free(cString);
  return buildedString;
}

//Returns the rest of the string 'str' from after the first appearence of 'delim'.
//container must hold a reference to a non null reference (not Empty string).
// str must be non null
char *tokenizer(char * str,char ** container_ptr,char delim){
  char c;
  char * tempContainer = 0;
  while(*str != '\0' && (c = *str) != delim ){
    //printf("in loop");
    //fflush();
    tempContainer = appendChar(*container_ptr,c);
    str++;
    free(*container_ptr);
    *container_ptr = tempContainer;
  }
  return str;
}

//Returns an empty string 
char* getEmptyString(){
  char *str = (char *) malloc(1);
  *str = '\0';
  return str;
}
*/
